﻿#include "delegatecenter.h"
#include <QApplication>

EnumDelegate::EnumDelegate(QStringList strList, QObject *parent):QItemDelegate(parent)
{
    enumStringList = strList;
}

void EnumDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const
{
    QVariant value = index.data(Qt::FontRole);
    if(value.isValid() && !value.isNull()){
        option->font = qvariant_cast<QFont>(value).resolve(option->font);
        option->fontMetrics = QFontMetrics(option->font);
    }
    //对齐方式
    value = index.data(Qt::TextAlignmentRole);
    if(value.isValid() && !value.isNull())
        option->displayAlignment = Qt::Alignment(value.toInt());

    //option->displayAlignment = Qt::AlignCenter; 居中
    //前景色
    value = index.data(Qt::ForegroundRole);
    if(value.canConvert<QBrush>())
        option->palette.setBrush(QPalette::Text,qvariant_cast<QBrush>(value));

    option->features |= QStyleOptionViewItem::HasDisplay;
    option->text = getDisplayText(index);

}

QString EnumDelegate::getDisplayText(const QModelIndex &index) const
{
    int enumNum = index.data().toInt();
    if(enumNum >= enumStringList.size() || enumNum < 0)
        return QString::fromLocal8Bit("未知");
    else
        return enumStringList.at(enumNum);
}

void EnumDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if(index.isValid()){
        Q_ASSERT(index.isValid());

        QStyleOptionViewItem opt = option;
        //去掉焦点
        opt.state &= ~QStyle::State_HasFocus;
        initStyleOption(&opt,index);
        const QWidget *widget = opt.widget;
        QStyle *style = widget ? widget->style() : QApplication::style();
        style->drawControl(QStyle::CE_ItemViewItem,&opt,painter,widget);
    }else{
        QItemDelegate::paint(painter,option,index);
    }
}

NumberDelegate::NumberDelegate(int m_precision, QObject *parent)
{
    precision = m_precision;
}

void NumberDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const
{
    QVariant value = index.data(Qt::FontRole);
    if(value.isValid() && !value.isNull()){
        option->font = qvariant_cast<QFont>(value).resolve(option->font);
        option->fontMetrics = QFontMetrics(option->font);
    }
    //对齐方式
    value = index.data(Qt::TextAlignmentRole);
    if(value.isValid() && !value.isNull())
        option->displayAlignment = Qt::Alignment(value.toInt());

    //option->displayAlignment = Qt::AlignCenter; 居中
    //前景色
    value = index.data(Qt::ForegroundRole);
    if(value.canConvert<QBrush>())
        option->palette.setBrush(QPalette::Text,qvariant_cast<QBrush>(value));

    option->features |= QStyleOptionViewItem::HasDisplay;
    option->text = getDisplayText(index);
}

QString NumberDelegate::getDisplayText(const QModelIndex &index) const
{
    double val = index.data().toDouble();
    return QString::number(val,'f',precision);
}

void NumberDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if(index.isValid()){
        Q_ASSERT(index.isValid());

        QStyleOptionViewItem opt = option;
        //去掉焦点
        opt.state &= ~QStyle::State_HasFocus;
        initStyleOption(&opt,index);
        const QWidget *widget = opt.widget;
        QStyle *style = widget ? widget->style() : QApplication::style();
        style->drawControl(QStyle::CE_ItemViewItem,&opt,painter,widget);
    }else{
        QItemDelegate::paint(painter,option,index);
    }
}
